/*
 * 
 *
 * Created on 24 de abril de 2008, 06:49 PM
 */
package org.morosidad.general;


import org.mybeans.Sesion;

import org.mybeans.TemporalMorosidadMensualBeans;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import org.mybeans.MorosidadBeans;
import org.mybeans.RemolquesBeans;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;
import org.remolques.busquedas.RemolquesBusquedaConPlacaFrame;

/**
 *
 * @author  avbravo
 */
public class MorosidadRemolquesFrame extends javax.swing.JFrame {

    String dueno;
    double morosidad;
    double recargo;
    double numeroporcentaje;
    int numeromes;
    double total;
    Sesion sesion;
    String sql;
    String tiposql;
    String idRegimen;
    String Regimen;
  //  String tipoid;
  //  String localId;
    double TotalMorosidad;
    double TotalRecargo;
    double GranTotal;
    double TotalDebe;
    
    boolean focoRemolques = false;
    RemolquesBeans remolquesBeans;
     int IdRemolque;
    String Placa;
    String estatus;
  String Mes;
    TemporalMorosidadMensualBeans temporalMorosidadMensualBeans;
    MorosidadBeans morosidadBeans;
    int fila = -1;
    DefaultTableModel modelo;
    int Anio;
    int MesFecha;
    static List<String> ListaRecargoId = new ArrayList<String>();
    static List<String> ListaRecargoTipoId = new ArrayList<String>();
    static List<String> ListaRecargoIdRegimen = new ArrayList<String>();
    int NumeroRegistrosRecargosEncontrados = -1;

    public String getDueno() {
        return dueno;
    }

    public void setDueno(String dueno) {
        this.dueno = dueno;
    }

    public double getMorosidad() {
        return morosidad;
    }

    public void setMorosidad(double morosidad) {
        this.morosidad = morosidad;
    }

    public int getNumeromes() {
        return numeromes;
    }

    public void setNumeromes(int numeromes) {
        this.numeromes = numeromes;
    }

    public int getAnio() {
        return Anio;
    }

    public void setAnio(int Anio) {
        this.Anio = Anio;
    }

    public double getNumeroporcentaje() {
        return numeroporcentaje;
    }

    public void setNumeroporcentaje(double numeroporcentaje) {
        this.numeroporcentaje = numeroporcentaje;
    }

    public double getRecargo() {
        return recargo;
    }

    public void setRecargo(double recargo) {
        this.recargo = recargo;
    }

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    /** Creates new form  */
    public MorosidadRemolquesFrame() {
        initComponents();
        /*
         * Fondo
         */

         
         
         

        jPanelFondo.setOpaque(false);
        setContentPane(jPanelFondo);
        /*
         * fondo
         */
        sesion = new Sesion();

        remolquesBeans = new RemolquesBeans();
        temporalMorosidadMensualBeans = new TemporalMorosidadMensualBeans();

        morosidadBeans = new MorosidadBeans();
        java.util.Calendar ca = java.util.Calendar.getInstance();
        java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());
        jFormattedTextFieldFecha.setValue(mydate);
        int year = ca.get(java.util.Calendar.YEAR);
        this.Anio = year;
        this.MesFecha = ca.get(java.util.Calendar.MONTH);
        jTextFieldAnio.setText(String.valueOf(year));

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelFondo = new javax.swing.JPanel();
        jButtonRegresar = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        jTextFieldAnio = new javax.swing.JTextField();
        jButtonMostrar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jFormattedTextFieldFecha = new javax.swing.JFormattedTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButtonVistaPrevia = new javax.swing.JButton();
        jButtonImprimir = new javax.swing.JButton();
        jButtonRegresar2 = new javax.swing.JButton();
        jCheckBoxIncluirRecargo = new javax.swing.JCheckBox();
        jPanel1 = new javax.swing.JPanel();
        jTextFieldGranTotal = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldPlaca = new javax.swing.JTextField();
        jTextFieldRemolque = new javax.swing.JTextField();
        jButtonBuscar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(" Morosidad  Remolques");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanelFondo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Cerrar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jLabel14.setText("Año");

        jButtonMostrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/tools.png"))); // NOI18N
        jButtonMostrar.setToolTipText("Procesar");
        jButtonMostrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMostrarActionPerformed(evt);
            }
        });

        jLabel1.setText("Fecha");

        jFormattedTextFieldFecha.setEnabled(false);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "IdRegimen", "Regimen", "Morosidad", "Estatus"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Double.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButtonVistaPrevia.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/preview.png"))); // NOI18N
        jButtonVistaPrevia.setToolTipText("Vista Previa");
        jButtonVistaPrevia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVistaPreviaActionPerformed(evt);
            }
        });

        jButtonImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/print.jpeg"))); // NOI18N
        jButtonImprimir.setToolTipText("Imprimir");
        jButtonImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimirActionPerformed(evt);
            }
        });

        jButtonRegresar2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar2.setToolTipText("Regresar");
        jButtonRegresar2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresar2ActionPerformed(evt);
            }
        });

        jCheckBoxIncluirRecargo.setSelected(true);
        jCheckBoxIncluirRecargo.setText("Incluir recargo");

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jTextFieldGranTotal.setEnabled(false);

        jLabel4.setText("Gran Total");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addGap(53, 53, 53)
                .addComponent(jTextFieldGranTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldGranTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jLabel5.setText("Placa");

        jLabel6.setText("Remolque");

        jTextFieldPlaca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldPlacaActionPerformed(evt);
            }
        });

        jTextFieldRemolque.setEnabled(false);
        jTextFieldRemolque.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldRemolqueActionPerformed(evt);
            }
        });

        jButtonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/search.png"))); // NOI18N
        jButtonBuscar.setToolTipText("Buscar");
        jButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelFondoLayout = new javax.swing.GroupLayout(jPanelFondo);
        jPanelFondo.setLayout(jPanelFondoLayout);
        jPanelFondoLayout.setHorizontalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addGap(21, 21, 21)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addComponent(jTextFieldPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(64, 64, 64)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFormattedTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTextFieldRemolque, javax.swing.GroupLayout.PREFERRED_SIZE, 446, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(258, 258, 258))
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jCheckBoxIncluirRecargo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel14)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldAnio, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButtonMostrar, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(387, Short.MAX_VALUE))
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 802, Short.MAX_VALUE)
                .addGap(24, 24, 24))
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jButtonVistaPrevia, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addComponent(jButtonRegresar2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(198, 198, 198))
        );
        jPanelFondoLayout.setVerticalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(jTextFieldPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonBuscar))
                    .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jFormattedTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextFieldRemolque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jCheckBoxIncluirRecargo)
                        .addComponent(jLabel14)
                        .addComponent(jTextFieldAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButtonMostrar)
                    .addComponent(jButtonRegresar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonVistaPrevia, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonRegresar2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10))
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(48, 48, 48))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelFondo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelFondo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jButtonRegresar2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresar2ActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresar2ActionPerformed

    @SuppressWarnings("static-access")
private void jButtonMostrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMostrarActionPerformed

    try {
        LimpiarTable();
        this.TotalMorosidad = 0.0;
        this.TotalRecargo = 0.0;
        this.GranTotal = 0.0;
        String SAnio = jTextFieldAnio.getText();
        this.Placa = jTextFieldPlaca.getText();

        if (this.Placa == null || this.Placa.equals("")) {
            JOptionPane.showMessageDialog(this, "No ha seleccionado un remolque ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldPlaca.requestFocus();
            return;
        }
        String remolque = jTextFieldRemolque.getText();
        if (remolque == null || remolque.equals("")) {
            JOptionPane.showMessageDialog(this, "No ha seleccionado un remolque ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldPlaca.requestFocus();
            return;
        }
        if (SAnio == null || SAnio.equals("")) {
            JOptionPane.showMessageDialog(this, "No ha seleccionado el año ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldAnio.requestFocus();
            return;
        }
        this.Anio = Integer.parseInt(SAnio);
        /*
         * Se le suma uno al año para que genere los 
         * impuestos para el siguiente año
         */

        if (this.Anio <= 2007) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un año valido ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldAnio.requestFocus();
            return;
        }



        sesion.LimpiarTemporalMorosidadMensual();
       morosidadBeans.setAnio(Anio);
       this.Mes = sesion.getNombreMes( this.MesFecha);
       morosidadBeans.setMes(this.Mes);
        morosidadBeans.VerificarMorosidad(String.valueOf(this.IdRemolque), "remolques", true);
        if (morosidadBeans.getNumeroRegistrosLista() == -1) {
            JOptionPane.showMessageDialog(this, "Este remolque no tiene morosidad", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        CargarMorosidad();
        CargarTabla();

         DecimalFormat formateador = new DecimalFormat("#########.##");

        jTextFieldGranTotal.setText(formateador.format(this.GranTotal));
        setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));


        setCursor(null);
        // JOptionPane.showMessageDialog(this, "Proceso terminado", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonMostrarActionPerformed

private void jButtonVistaPreviaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVistaPreviaActionPerformed



    String reportSource;
    reportSource = sesion.getRutaReportes() + "MorosidadRemolques.jrxml";
    String reportDest;
    reportDest = sesion.getRutaDestino() + "MorosidadRemolques.html";

    Map<String, Object> params = new HashMap<String, Object>();

    try {
        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        JasperReport jasperReport =
                JasperCompileManager.compileReport(reportSource);
        params.put("P_IDMUNICIPIO", sesion.getIdmunicipio());
        params.put("P_USERNAME", sesion.getUsername());
        params.put("P_MUNICIPIO", sesion.getMunicipio());
        params.put("P_ANIO", this.Anio);



        JasperPrint jasperPrint =
                JasperFillManager.fillReport(
                jasperReport, params, sesion.con);

        JasperViewer.viewReport(jasperPrint, false);

    } catch (JRException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonVistaPreviaActionPerformed

private void jButtonImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimirActionPerformed



    String reportSource;
    reportSource = sesion.getRutaReportes() + "MorosidadRemolques.jrxml";
    String reportDest;
    reportDest = sesion.getRutaDestino() + "MorosidadRemolques.html";

    Map<String, Object> params = new HashMap<String, Object>();

    try {
        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        JasperReport jasperReport =
                JasperCompileManager.compileReport(reportSource);
        params.put("P_IDMUNICIPIO", sesion.getIdmunicipio());
        params.put("P_USERNAME", sesion.getUsername());
        params.put("P_MUNICIPIO", sesion.getMunicipio());
        params.put("P_ANIO", this.Anio);



        JasperPrint jasperPrint =
                JasperFillManager.fillReport(
                jasperReport, params, sesion.con);

        JasperViewer.viewReport(jasperPrint, true);

    } catch (JRException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonImprimirActionPerformed

private void jTextFieldRemolqueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldRemolqueActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_jTextFieldRemolqueActionPerformed

private void jTextFieldPlacaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldPlacaActionPerformed
    // TODO add your handling code here:
    ValidarRemolques();
}//GEN-LAST:event_jTextFieldPlacaActionPerformed

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    // TODO add your handling code here:
    try {

        if (focoRemolques) {
            GranTotal = 0.0;
            ActualizarRemolques();
        }

        focoRemolques = false;
    } catch (Exception ex) {
        sesion.MostrarMensajeError("Error ", "Boton Actualizar Impuestos()\n " + ex.getMessage().toString());
    }
}//GEN-LAST:event_formWindowGainedFocus

private void jButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarActionPerformed
    // TODO add your handling code here:
     RemolquesBusquedaConPlacaFrame remolquesBusquedaConPlacaFrame = new RemolquesBusquedaConPlacaFrame();
    remolquesBusquedaConPlacaFrame.setLocationRelativeTo(null);
    remolquesBusquedaConPlacaFrame.setVisible(true);
    focoRemolques = true;
}//GEN-LAST:event_jButtonBuscarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MorosidadRemolquesFrame().setVisible(true);
            }
        });
    }

        private void ValidarRemolques() {
        try {
            LimpiarTable();
            TotalDebe = 0.0;

            this.Placa = this.jTextFieldPlaca.getText();
            if (Placa == null || Placa.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese la placa del remolque", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;

            }


            remolquesBeans.setPlaca(Placa);

            if (remolquesBeans.ConsultarPorPlaca() == true) {

                if (remolquesBeans.getPlaca() == null || remolquesBeans.getPlaca().equals("")) {
                    JOptionPane.showMessageDialog(this, "Este remolque no tiene asiganda una placa...", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                this.IdRemolque = remolquesBeans.getIdRemolque();
                CargarRemolques();

            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "ValidarRemolques()\nError " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void ActualizarRemolques() {
        try {
            CargarRemolques();
            LimpiarTable();
 
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "ActualizarRemolques()\nError  " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

 public void CargarRemolques() {
        try {

            if (remolquesBeans.getActivo().equals("no")) {
                JOptionPane.showMessageDialog(this, "Este registro esta inactivo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            this.jTextFieldPlaca.setText(remolquesBeans.getPlaca());
            sesion.setIdRemolque(this.IdRemolque);

            this.IdRemolque = remolquesBeans.getIdRemolque();
           String dueno = "";
            if (remolquesBeans.getDueno() == null) {
                dueno = "";
            } else {
                dueno = " Dueño " + remolquesBeans.getDueno();
            }

            jTextFieldRemolque.setText(remolquesBeans.getMarca() + " Modelo: " + remolquesBeans.getModelo() + dueno);
            repaint();


        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "CargarRemolques()\n " + ex.getMessage().toString());
        }
    }

    @SuppressWarnings({"static-access", "static-access", "static-access"})
    public void CargarMorosidad() {
        try {

            this.NumeroRegistrosRecargosEncontrados = -1;
            ListaRecargoId.removeAll(ListaRecargoId);
            ListaRecargoTipoId.removeAll(ListaRecargoTipoId);
            ListaRecargoIdRegimen.removeAll(ListaRecargoIdRegimen);
            if (morosidadBeans.getNumeroRegistrosLista() != - 1) {
                for (int i = 0; i <= morosidadBeans.getNumeroRegistrosLista(); i++) {

                    this.morosidad = morosidadBeans.getListaMorosidad().get(i);
                    this.idRegimen = morosidadBeans.getListaIdRegimen().get(i);
                    this.Regimen = morosidadBeans.getListaRegimen().get(i);
                    // busco si tiene recargos de ese impuesto
                    this.estatus = morosidadBeans.getListaEstatus().get(i);
                    this.GranTotal += this.morosidad;

                    GuardarRegistroTablaTemporal();

                }//for
            }





        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "CargarMorosidad()\nError " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void GuardarRegistroTablaTemporal() {
        try {
            temporalMorosidadMensualBeans.setAnio(this.Anio);
            temporalMorosidadMensualBeans.setMes("");
            temporalMorosidadMensualBeans.setId(String.valueOf(this.IdRemolque));
            temporalMorosidadMensualBeans.setTipoId("remolques");
            temporalMorosidadMensualBeans.setPropietario(this.dueno);
            temporalMorosidadMensualBeans.setDireccion("");
            temporalMorosidadMensualBeans.setRegimen(this.Regimen);
            temporalMorosidadMensualBeans.setIdRegimen(this.idRegimen);
            temporalMorosidadMensualBeans.setMorosidad(this.morosidad);
            temporalMorosidadMensualBeans.setRecargo(this.recargo);
            temporalMorosidadMensualBeans.setTotal(this.total);
            temporalMorosidadMensualBeans.setIdMunicipio(sesion.getIdmunicipio());
            temporalMorosidadMensualBeans.setNumeroMes(this.numeromes);
            temporalMorosidadMensualBeans.setNumeroPorcentaje(this.numeroporcentaje);
            temporalMorosidadMensualBeans.setTotalMorosidad(TotalMorosidad);
            temporalMorosidadMensualBeans.setTotalRecargo(TotalRecargo);
            temporalMorosidadMensualBeans.setEstatus(estatus);
            temporalMorosidadMensualBeans.setGranTotal(GranTotal);

temporalMorosidadMensualBeans.setPlaca(this.Placa);
temporalMorosidadMensualBeans.setIdCorregimiento("");
temporalMorosidadMensualBeans.setCorregimiento("");
temporalMorosidadMensualBeans.setTelefono("");
            temporalMorosidadMensualBeans.Agregar();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "GuardarRegistroTablaTemporal()\nError " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void LimpiarTable() {
        try {
            //limpiar  el jXTable

            jTextFieldGranTotal.setText("");
            boolean control = false;
            while (control == false) {
                int n = jTable1.getRowCount();

                if (n <= 0) {
                    control = true;
                } else {
                    modelo.removeRow(0);
                }

            }//while
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "LimpiarTable()\n " + ex.getMessage().toString());
        }
    }

    private void CargarTabla() {
        try {
            sql = "select temporalmorosidadmensual.* from  temporalmorosidadmensual  where idmunicipio=? and username=?";
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            PreparedStatement psquery = sesion.con.prepareStatement(sql);
            psquery.setObject(1, sesion.getIdmunicipio());
            psquery.setObject(2, sesion.getUsername()); //username
            ResultSet rs = psquery.executeQuery();

            modelo = new DefaultTableModel();
            // Creamos las columnas.
            //  modelo.addColumn("id");
            //  modelo.addColumn("propietario");
            modelo.addColumn("idregimen");
            modelo.addColumn("regimen");
            modelo.addColumn("morosidad");
            modelo.addColumn("estatus");

// Bucle para cada resultado en la consulta
            while (rs.next()) {
                // Se crea un array que será una de las filas de la tabla.
                Object[] fila = new Object[4]; // Hay tres columnas en la tabla

                //    fila[0] = rs.getString("id");
                //  fila[1] = rs.getString("propietario");
                fila[0] = rs.getString("idregimen");
                fila[1] = rs.getString("regimen");
                fila[2] = rs.getString("morosidad");
                fila[3] = rs.getString("estatus");

                modelo.addRow(fila);

            }
            this.jTable1.setModel(modelo);
            jTable1.setVisible(true);

            this.jTable1.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {

                    fila = jTable1.rowAtPoint(e.getPoint());
                    int columna = jTable1.columnAtPoint(e.getPoint());
                    if ((fila > -1) && (columna > -1)) {
                    }
                }
            });
            TableColumn columnIdRegimen = jTable1.getColumnModel().getColumn(0);
            columnIdRegimen.setPreferredWidth(40);
            TableColumn columnRegimen = jTable1.getColumnModel().getColumn(1);
            columnRegimen.setPreferredWidth(90);
            TableColumn columnMorosidad = jTable1.getColumnModel().getColumn(2);
            columnMorosidad.setPreferredWidth(40);
            TableColumn columnMeses = jTable1.getColumnModel().getColumn(3);
            columnMeses.setPreferredWidth(40);
            sesion.con.close();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "CargarTabla()\nError " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JButton jButtonImprimir;
    private javax.swing.JButton jButtonMostrar;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JButton jButtonRegresar2;
    private javax.swing.JButton jButtonVistaPrevia;
    private javax.swing.JCheckBox jCheckBoxIncluirRecargo;
    private javax.swing.JFormattedTextField jFormattedTextFieldFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanelFondo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldAnio;
    private javax.swing.JTextField jTextFieldGranTotal;
    private javax.swing.JTextField jTextFieldPlaca;
    private javax.swing.JTextField jTextFieldRemolque;
    // End of variables declaration//GEN-END:variables
}
